package cn.timeapi.Applet.Mall.CatSquare.controller.backend;

import cn.timeapi.Applet.Mall.CatSquare.common.ResponseCode;
import cn.timeapi.Applet.Mall.CatSquare.dao.ManagementAccountMapper;
import cn.timeapi.Applet.Mall.CatSquare.service.IManagementAccountService;
import cn.timeapi.Applet.Mall.CatSquare.common.Const;
import cn.timeapi.Applet.Mall.CatSquare.common.ParamsNotNull;
import cn.timeapi.Applet.Mall.CatSquare.common.ServerResponse;
import cn.timeapi.Applet.Mall.CatSquare.entity.ManagementAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 描述:后台登录的用户
 * @author Vincent Vic
 * create 2020-02-15 12:53
 */

@Api("/manage/account/")
@RestController
@RequestMapping("/manage/account/")   //请求地址设置
public class AccountManageController {

    @Autowired
    private IManagementAccountService iManagementAccountService;

    @Autowired
    private ManagementAccountMapper managementAccountMapper;

    /**
     * 后台登入
     * @param phone 登入手机号
     * @param passwoed 密码
     * @param session 会话
     * @return
     */
    @ApiOperation("后台登入")
    @GetMapping("login")
    @ResponseBody   //自动序列化Json
    public ServerResponse<ManagementAccount> login(@ApiParam("账户(手机号)") @ParamsNotNull String phone , @ApiParam("密码") @ParamsNotNull String passwoed, HttpSession session){
        //server->mybatis->dao
        ServerResponse<ManagementAccount> response = iManagementAccountService.login(phone,passwoed);

        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 登出
     * @param session 会话
     * @return 上层数据
     */
    @ApiOperation("登出")
    @GetMapping("logou")
    @ResponseBody   //自动序列化Json
    public Object logou(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.success();
    }

    /**
     * 后台注册功能 仅限管理员
     * @param ManagementAccount 用户信息
     * @return 上层数据
     */
    @ApiOperation("后台注册功能 仅限管理员")
    @GetMapping("register")
    @ResponseBody   //自动序列化Json
    public ServerResponse<String> register(HttpSession session,@ApiParam("账户属性") ManagementAccount ManagementAccount){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        if (!iManagementAccountService.checkAdminRole(currentuser).isSuccess()){
            return ServerResponse.errorMsg("不是管理员，不能修改");
        }
        return iManagementAccountService.register(ManagementAccount);
    }

    /**
     * 校验手机号/账户
     * @param phone 电话兼账号
     * @return 上层数据
     */
    @GetMapping("校验手机号/账户")
    @ResponseBody   //自动序列化Json
    public ServerResponse<String> checkValid(@ApiParam("账户/手机号") @ParamsNotNull String phone){
        return iManagementAccountService.checkValid(phone);
    }

    /**
     * 获取用户信息
     * @param session 会话
     * @return 上层数据/用户未登录
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getUserInfo")
    @ResponseBody   //自动序列化Json
    public Object getUserInfo(HttpSession session){
        //获取当前用户
        ManagementAccount ManagementAccount = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (ManagementAccount != null){
            return ServerResponse.success(ManagementAccount);
        }
        return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
    }

    /**
     * 员工重置密码
     * @param session 会话
     * @param passwordOld 旧密码
     * @param passwordNew   新密码
     * @return 上层数据
     */
    @ApiOperation("员工重置密码")
    @GetMapping("RestPasword")
    @ResponseBody   //自动序列化Json
    public ServerResponse<String> RestPasword(HttpSession session,@ApiParam("旧密码") @ParamsNotNull String passwordOld,@ApiParam("新密码") @ParamsNotNull String passwordNew){
        //获取当前用户
        ManagementAccount ManagementAccount = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (ManagementAccount == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        return iManagementAccountService.restPasword(ManagementAccount.getPhone(), DigestUtils.md5Hex(passwordOld.getBytes()),passwordNew);
    }

    /**
     * 管理员重置密码
     * @param session 会话
     * @param phone 账号
     * @param passwordNew   新密码
     * @return 上层数据
     */
    @ApiOperation("管理员重置密码")
    @GetMapping("RestPaswordByAdmin")
    @ResponseBody   //自动序列化Json
    public ServerResponse<String> RestPaswordByAdmin(HttpSession session,@ApiParam("手机号/账户") @ParamsNotNull String phone,@ApiParam("密码")  @ParamsNotNull String passwordNew){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }

        if (!iManagementAccountService.checkAdminRole(currentuser).isSuccess()){
            return ServerResponse.errorMsg("不是管理员，不能修改");
        }

        ManagementAccount managementAccount = managementAccountMapper.selectByPhone(phone);

        return iManagementAccountService.restPasword(currentuser.getPhone(), managementAccount.getPassword(),passwordNew);
    }

    /**
     * 更新手机号
     * @param session
     * @param oldPhone
     * @param newPhone
     * @return
     */
    @ApiOperation("更新手机号")
    @GetMapping("updata_phone")
    @ResponseBody   //自动序列化Json
    public ServerResponse<String> updata_phone(HttpSession session, @ApiParam("旧手机号") @ParamsNotNull String oldPhone,@ApiParam("新手机号") @ParamsNotNull String newPhone){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        if (!iManagementAccountService.checkAdminRole(currentuser).isSuccess()){
            return ServerResponse.errorMsg("不是管理员，不能修改");
        }

        ServerResponse<ManagementAccount> response = iManagementAccountService.updataAccount(oldPhone,newPhone);
        if (response.isSuccess()){
            return ServerResponse.success("更新成功");
        }
        return ServerResponse.errorMsg("更新失败");
    }
}